﻿using coreapi.Authodtion;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Models;
using SqlSugar;
using coreapi.Helpers;
using coreapi.Models.Enum;

namespace coreapi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "v2")]
    public class LoginController : ControllerBase
    {
        static IAuthenticateService authService;
        private readonly ISqlSugarClient db;

        /// <param name="db"></param>
        /// <param name="_authService"></param>
        public LoginController(ISqlSugarClient db, IAuthenticateService _authService)
        {
            authService = _authService;
            this.db = db;
        }
        //生成模型
        //[HttpGet(Name = "GetValue")]
        //public object Get()
        //{
        //    db.DbFirst.CreateClassFile("D:\\桌面\\GOO\\heartless-years\\coreapi\\coreapi\\Models", "Models");
        //    return "";
        //}
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Login(string? name, string? pwd)
        {
            //EmployeeUser s = db.Queryable<EmployeeUser>().Where(a => a.UName == name && a.UPhone == pwd).First();
            EmployeeUser s = db.Queryable<EmployeeUser>().Where(a => a.UName == name).First();
            var ret = new WebApiCallBack();

            try
            {
                if (string.IsNullOrEmpty(name) ||string.IsNullOrEmpty(pwd)) {
                    //用户不存在
                    ret.status = false;
                    ret.code = (int)ReturnEnum.Error;
                    ret.msg = "账号或密码不能为空！";
                    ret.otherData = new
                    {
                        name,
                        pwd
                    };
                    return Ok(ret);
                }


                if (s == null)
                {
                    //用户不存在
                    ret.status = false;
                    ret.code = (int)ReturnEnum.Error;
                    ret.msg = "该用户不存在！";
                    ret.otherData = new
                    {
                        name,
                        pwd
                    };
                    return Ok(ret);
                }


                //判断账号密码
                if (s.UPassword != MD5Help.MD5Sign(pwd))
                {
                    ret.status = false;
                    ret.code = (int)ReturnEnum.Error;
                    ret.msg = "密码错误！";
                    ret.otherData = new
                    {
                        name,
                        pwd
                    };
                    return Ok(ret);
                }

                string token;
                if (authService.IsAuthenticated(s, out token))
                {
                    token = token.Trim();
                }
                ret.status = true;
                ret.code = (int)ReturnEnum.Success;
                ret.msg = "登陆成功！";
                ret.otherData = new
                {  // pwd = s.UPhone,
                    name = name,
                    token = token,
                };
                return Ok(ret);

            }

            catch (Exception ex)
            {
                ret.status = false;
                ret.code = (int)ReturnEnum.Error;
                ret.msg = ex.ToString();
                ret.otherData = new
                {
                    name,
                    pwd
                };
                return Ok(ret);

            }





        }




    }
}
